package cn.moukou.sbzz.controller;

import cn.moukou.sbzz.pojo.OrderFromEnterprise;
import cn.moukou.sbzz.service.OrderFromEnterpriseService;
import cn.moukou.sbzz.util.JwtUtil;
import cn.moukou.sbzz.util.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/sbzz/api")
public class OrderFromEnterpriseController {

    @Autowired
    OrderFromEnterpriseService service;

    @PostMapping("/bank-product/order/make")
    public Resp<String> makeOneOrder(HttpServletRequest request,
                                     @RequestParam("bid") String bid,
                                     @RequestParam("pid") String pid,
                                     @RequestParam("detail") String detail,
                                     @RequestParam("productName") String productName) throws Exception {
        String eid = JwtUtil.getUid(request);
        if(service.makeOrder(bid, eid, pid, detail, productName) > 0) {
            return Resp.ok("购买成功");
        } else {
            return Resp.fail("购买失败");
        }
    }

    @GetMapping("/bank-product/order/one")
    public Resp<OrderFromEnterprise> getOneOrder(@RequestParam("id") String id) {
        return Resp.ok(service.getOneOrder(id));
    }

    @PostMapping("/bank-product/order/auth")
    public Resp<String> authOneOrder(@RequestParam("id") String id) {
        if(service.authOrder(id) > 0) {
            return Resp.ok("接洽成功");
        } else {
            return Resp.fail("接洽失败");
        }
    }

    @GetMapping("/bank-product/order/from-me")
    public Resp<List<OrderFromEnterprise>> getOrderFromMe(HttpServletRequest request,
                                                          @RequestParam("status") int status) throws Exception {
        String eid = JwtUtil.getUid(request);
        return Resp.ok(service.getOrderFromMe(eid, status));
    }

    @GetMapping("/bank-product/order/to-me")
    public Resp<List<OrderFromEnterprise>> getOrderToMe(HttpServletRequest request,
                                                        @RequestParam("status") int status) throws Exception {
        String bid = JwtUtil.getUid(request);
        return Resp.ok(service.getOrderToMe(bid, status));
    }

}
